package br.ufrgs.inf.engsoft.stp.view.user;

import java.util.List;

import br.ufrgs.inf.engsoft.stp.user.User;
import br.ufrgs.inf.engsoft.stp.util.data.IndexedContainerUtil;

import com.vaadin.data.util.IndexedContainer;
import com.vaadin.ui.Table;

@SuppressWarnings("serial")
public class UserTable extends Table {

    public static String[] fields = { "CPF", "Nome", "Perfil" }; 
    public static String[] sortfields = { "CPF" };
    private static boolean sortEnable[] = { true };

    private IndexedContainer usersData = IndexedContainerUtil.createDummyData(fields);

    public UserTable() {
        setContainerDataSource(usersData);
        setSelectable(true);
        setImmediate(true);
        setVisibleColumns(fields);
    }    

    public void cleanTable() {
        if (size() > 0) {
            removeAllItems();
        }
        select(null);
    }
    
    public void updateTable(List<User> list) {
        cleanTable();
        if(list != null) {
	        for (User user : list) {
	            Object id = ((IndexedContainer) getContainerDataSource()).addItem();
	            getItem(id).getItemProperty(UserTable.fields[0]).setValue(
	            		user.getCpf());
	            getItem(id).getItemProperty(UserTable.fields[1]).setValue(
	                    user.getName());
	            getItem(id).getItemProperty(UserTable.fields[2]).setValue(
	                    user.getRole().getName());
	        }
	        sortTable();
        }
    }

    public void sortTable() {
        sort(sortfields, sortEnable);
    }
}